package cn.ranko.shiro.controller;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.model.SysUserRole;
import cn.ranko.shiro.realm.ShiroDbRealm;
import cn.ranko.shiro.service.SysRoleService;
import cn.ranko.shiro.service.SysUserService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by user on 2017/8/25.
 */
@Controller
@RequestMapping("/shiro/user")
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;

    @RequiresPermissions("user")
    @RequestMapping("/index.action")
    public String index(ModelMap modelMap){
        return "user/index";
    }

    @RequiresPermissions("user")
    @RequestMapping("/loadList.action")
    @ResponseBody
    public Datagrid loadList(Integer page,Integer rows){
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setRows(rows);
        return sysUserService.loadList(pagination);
    }

    @RequiresPermissions(value = {"user_add","user_delete"},logical = Logical.OR)
    @RequestMapping("/showedit.action")
    public String showedit(Integer user_id,ModelMap modelMap){
        if(user_id==0){
            //添加
        }else{
            //修改
            SysUser sysUser = sysUserService.selectByUserId(user_id);
            modelMap.addAttribute("sysUser",sysUser);

            List<SysUserRole> sysUserRoles = sysRoleService.getSysUserRoleByUserId(user_id);
            modelMap.addAttribute("sysUserRoles",sysUserRoles);
        }
        return "user/showedit";
    }

    @RequiresPermissions(value = {"user_add","user_delete"},logical = Logical.OR)
    @RequestMapping("/save.action")
    @ResponseBody
    public String save(SysUser user,String roleIds){
        int affect = sysUserService.save(user,roleIds);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

    @RequiresPermissions("user_delete")
    @RequestMapping("/delete.action")
    @ResponseBody
    public String delete(Integer user_id){
        int affect = sysUserService.delete(user_id);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

    @RequiresPermissions("user_resetpassword")
    @RequestMapping("/updatePassword.action")
    @ResponseBody
    public String updatePassword(String password){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(ShiroDbRealm.SESSION_USER_KEY);
        int affect = sysUserService.updatePassword(sysUser,password);
        if(affect>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else {
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

}
